package com.ywq.app.dao.impl;

import java.util.List;
import java.util.Map;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ywq.app.dao.ArtAuctionDao;
import com.ywq.app.domain.ArtAuction;
import com.ywq.app.domain.ArtAuctionPic;
import com.ywq.app.domain.ArtPic;
import com.ywq.app.domain.ArtReminder;
import com.ywq.app.domain.PicBidLog;
import com.ywq.common.exception.AppBusinessException;

public class ArtAuctionDaoImpl extends SqlSessionDaoSupport implements ArtAuctionDao{
	
	private static final Logger logger = LoggerFactory.getLogger(ArtAuctionDaoImpl.class);
	
	/**
	 * 拍卖主题总数
	 */
	@Override
	public int getArtAuctionListCount(Map<String, Object> map)throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"com.ywq.app.persistence.ArtAuctionMapper.getArtAuctionListCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getArtAuctionListCount user failure.", e);
			}
			throw new AppBusinessException("getArtAuctionListCount user failure.", e);
		}
	}
	/**
	 * 主题列表
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<ArtAuction> getArtAuctionList(Map<String, Object> map)
			throws AppBusinessException {

		try {
			return getSqlSession()
					.selectList(
							"com.ywq.app.persistence.ArtAuctionMapper.getArtAuctionList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getArtAuctionList list failure.", e);
			}
			throw new AppBusinessException("getArtAuctionList list failure.", e);
		}
	}
	/**
	 * 新增主题
	 */
	@Override
	public void insertArtAuction(ArtAuction artAuction) throws AppBusinessException {
		try {
			getSqlSession()
					.insert("com.ywq.app.persistence.ArtAuctionMapper.insertArtAuction",
							artAuction);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertArtAuction user failure.", e);
			}
			throw new AppBusinessException("insertArtAuction user failure.", e);
		}
	}
	/**
	 * 更新主题
	 */
	@Override
	public void updateArtAuction(ArtAuction artAuction) throws AppBusinessException {
		try {
			getSqlSession()
					.update("com.ywq.app.persistence.ArtAuctionMapper.updateArtAuction",
							artAuction);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateArtAuction user failure.", e);
			}
			throw new AppBusinessException("updateArtAuction user failure.", e);
		}
	}
	//当前主题
	@SuppressWarnings("unchecked")
	@Override
	public List<ArtAuction> getNowAuctionList(Map<String, Object> map)throws AppBusinessException {

		try {
			return getSqlSession()
					.selectList(
							"com.ywq.app.persistence.ArtAuctionMapper.getNowAuctionList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getNowAuctionList list failure.", e);
			}
			throw new AppBusinessException("getNowAuctionList list failure.", e);
		}
	}
	//历史主题
	@SuppressWarnings("unchecked")
	@Override
	public List<ArtAuction> getOldAuctionList(Map<String, Object> map)throws AppBusinessException {

		try {
			return getSqlSession()
					.selectList(
							"com.ywq.app.persistence.ArtAuctionMapper.getOldAuctionList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getOldAuctionList list failure.", e);
			}
			throw new AppBusinessException("getOldAuctionList list failure.", e);
		}
	}
	//历史主题个数
	@Override
	public int getOldAuctionListCount(Map<String, Object> map)throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"com.ywq.app.persistence.ArtAuctionMapper.getOldAuctionListCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getOldAuctionListCount user failure.", e);
			}
			throw new AppBusinessException("getOldAuctionListCount user failure.", e);
		}
	}
	//单个主题
	@Override
	public ArtAuction getInAuctionById(Map<String, Object> map)throws AppBusinessException {

		try {
			return (ArtAuction) getSqlSession()
					.selectOne(
							"com.ywq.app.persistence.ArtAuctionMapper.getInAuctionById",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getInAuctionById list failure.", e);
			}
			throw new AppBusinessException("getInAuctionById list failure.", e);
		}
	}
	//添加主题与图片关联表
	@Override
	public void insertArtAuctionPic(ArtAuctionPic artAuctionPic) throws AppBusinessException {
		try {
			getSqlSession()
					.insert("com.ywq.app.persistence.ArtAuctionMapper.insertArtAuctionPic",
							artAuctionPic);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertArtAuctionPic user failure.", e);
			}
			throw new AppBusinessException("insertArtAuctionPic user failure.", e);
		}
	}
	//主题内展品数
	@SuppressWarnings("unchecked")
	@Override
	public List<ArtAuctionPic> getArtAuctionPicById(Map<String, Object> map)throws AppBusinessException {

		try {
			return getSqlSession()
					.selectList(
							"com.ywq.app.persistence.ArtAuctionMapper.getArtAuctionPicById",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getArtAuctionPicById list failure.", e);
			}
			throw new AppBusinessException("getArtAuctionPicById list failure.", e);
		}
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<ArtPic> getArtAuctionPicByAucId(Map<String, Object> map)throws AppBusinessException {

		try {
			return getSqlSession()
					.selectList(
							"com.ywq.app.persistence.ArtAuctionMapper.getArtAuctionPicByAucId",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getArtAuctionPicByAucId list failure.", e);
			}
			throw new AppBusinessException("getArtAuctionPicByAucId list failure.", e);
		}
	}
	
	//某主题作品总数
	@Override
	public int getArtAuctionPicByIdCount(Map<String, Object> map)throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"com.ywq.app.persistence.ArtAuctionMapper.getArtAuctionPicByIdCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getOldAuctionListCount user failure.", e);
			}
			throw new AppBusinessException("getOldAuctionListCount user failure.", e);
		}
	}
	@Override
	public PicBidLog getSumBidPirceLog(String auctionId)throws AppBusinessException {
		try {
			return (PicBidLog) getSqlSession()
			.selectOne(
					"com.ywq.app.persistence.ArtAuctionMapper.getSumBidPirceLog",
					auctionId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getSumBidPirceLog user failure.", e);
			}
			throw new AppBusinessException("getSumBidPirceLog user failure.", e);
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<PicBidLog> getLogListBid(Map<String, Object> map)throws AppBusinessException {

		try {
			return getSqlSession()
					.selectList(
							"com.ywq.app.persistence.ArtAuctionMapper.getLogListBid",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getLogListBid list failure.", e);
			}
			throw new AppBusinessException("getLogListBid list failure.", e);
		}
	}
	//
	@Override
	public void insertArtReminder(ArtReminder artReminder) throws AppBusinessException {
		try {
			getSqlSession()
					.insert("com.ywq.app.persistence.ReminderMapper.insterReminder",
							artReminder);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insterReminder user failure.", e);
			}
			throw new AppBusinessException("insterReminder user failure.", e);
		}
	}
	//删除
	@Override
	public void deleteAuctionPic(Map<String, Object> map) throws AppBusinessException {
		try {
			getSqlSession()
					.insert("com.ywq.app.persistence.ArtAuctionMapper.deleteAuctionPic",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("ArtAuctionMapper user failure.", e);
			}
			throw new AppBusinessException("ArtAuctionMapper user failure.", e);
		}
	}
	//删除图片
	@Override
	public void deletePic(Map<String, Object> map) throws AppBusinessException {
		try {
			getSqlSession()
					.insert("com.ywq.app.persistence.ArtAuctionMapper.deletePic",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deletePic user failure.", e);
			}
			throw new AppBusinessException("deletePic user failure.", e);
		}
	}
}
